package com.project.myPackage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.project.myPackage.common.R;
import com.project.myPackage.entity.Orderdetails;
import com.project.myPackage.service.OrderdetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "订单详情管理")
@Slf4j
@RestController
@RequestMapping("/")
public class OrderdetailsController {
    @Autowired
    private OrderdetailsService orderdetailService;

    @ApiOperation(value = "根据订单id获取所有订单详情信息")
    @RequestMapping("/getAllOrderdetailsByOrderId")
    public R<?> getAllOrderdetailsByOrderId(Integer order_id) {
        List<Map<String, Object>> list = orderdetailService.getAllOrderdetailsByOrderId(order_id);
        if (list != null) {
            return R.success(list, "获取所有订单详情信息列表成功！");
        } else {
            return R.error("获取所有订单详情信息列表失败！");
        }
    }

    @ApiOperation(value = "新增订单详情信息")
    @PostMapping("/addOrderdetails")
    public R<?> addOrderdetails(@RequestBody Orderdetails orderdetails) {
        boolean save = orderdetailService.save(orderdetails);
        if (save) {
            return R.success(orderdetails, "新增订单详情信息成功！");
        } else {
            return R.error("新增订单详情信息失败！");
        }
    }

    @ApiOperation(value = "批量新增订单详情信息")
    @PostMapping("/addOrderdetailsByList")
    public R<?> addOrderdetailsByList(@RequestBody List<Orderdetails> orderdetailsList) {
        boolean saveBatch = orderdetailService.saveBatch(orderdetailsList);
        if (saveBatch) {
            return R.success(orderdetailsList, "批量新增订单详情信息成功！");
        } else {
            return R.error("批量新增订单详情信息失败！");
        }
    }

    @ApiOperation(value = "删除订单详情信息")
    @RequestMapping("/deleteOrderdetails")
    public R<?> deleteOrderdetails(Integer order_detail_id) {
        boolean remove = orderdetailService.removeById(order_detail_id);
        if (remove) {
            return R.success(order_detail_id, "删除订单详情信息成功！");
        } else {
            return R.error("删除订单详情信息失败！");
        }
    }

    @ApiOperation(value = "修改订单详情信息")
    @PostMapping("/updateOrderdetails")
    public R<?> updateOrderdetails(@RequestBody Orderdetails orderdetails) {
        boolean update = orderdetailService.updateById(orderdetails);
        if (update) {
            return R.success(orderdetails, "修改订单详情信息成功！");
        } else {
            return R.error("修改订单详情信息失败！");
        }
    }

    @ApiOperation(value = "批量删除订单详情信息")
    @PostMapping("/deleteBatchOrderdetails")
    public R<?> deleteBatchOrderdetails(@RequestBody List<Integer> order_detail_ids) {
        boolean remove = orderdetailService.removeByIds(order_detail_ids);
        if (remove) {
            return R.success(order_detail_ids, "批量删除订单详情信息成功！");
        } else {
            return R.error("批量删除订单详情信息失败！");
        }
    }

    @ApiOperation(value = "根据order_id批量删除订单详情信息")
    @GetMapping("/deleteBatchOrderdetailsByOrderId")
    public R<?> deleteBatchOrderdetailsByOrderId(@RequestParam Integer order_id) {
        LambdaQueryWrapper<Orderdetails> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orderdetails::getOrder_id, order_id);
        List<Orderdetails> list1 = orderdetailService.list(queryWrapper);
        if(list1.size()>0){
            boolean remove = orderdetailService.remove(queryWrapper);
            if(remove){
                // 删除订单详情信息成功
                return R.success("删除订单信息成功！");
            }else{
                // 删除订单详情信息失败
                return R.error("删除订单信息失败！");
            }
        }else{
            return R.error("无订单可删除！");
        }
    }

    @ApiOperation(value = "根据订单详情统计商品分类下的销售总额可以根据时间段来筛选")
    @GetMapping("/getTotalCategoryMoney")
    public R<?> getTotalCategoryMoney(@RequestParam(required = false) String start_time,@RequestParam(required = false) String end_time) {
        //处理时间
        if(start_time!=null&&end_time!=null){
            start_time = start_time+" 00:00:00";
            end_time = end_time+" 23:59:59";
        }
        List<Map<String, Object>> list = orderdetailService.getTotalCategoryMoney(start_time,end_time);
        if (list != null) {
            return R.success(list, "获取各分类总销售额成功");
        } else {
            return R.error("获取各分类总销售额失败");
       }
    }

    @ApiOperation(value = "根据订单日期统计每日下的销售额 可根据日期来筛选")
    @GetMapping("/getTotalMoneyByOrderDate")
    public R<?> getTotalMoneyByOrderDate(@RequestParam(required = false) String start_time,@RequestParam(required = false) String end_time) {
       //"2025-04-06"
        List<Map<String, Object>> list = orderdetailService.getTotalMoneyByOrderDate(start_time,end_time);
        if (list != null) {
            return R.success(list, "获取数据成功");
        } else {
            return R.error("获取数据失败");
        }
    }

}
